/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TreeGenerator.Tools;

import com.jme3.asset.AssetManager;
import com.jme3.bounding.BoundingBox;
import com.jme3.material.Material;
import com.jme3.material.RenderState.BlendMode;
import com.jme3.math.FastMath;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.queue.RenderQueue.Bucket;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh;
import com.jme3.scene.Spatial;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.VertexBuffer.Format;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.VertexBuffer.Usage;
import com.jme3.texture.Texture;
import com.jme3.util.BufferUtils;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;

/**
 *
 * @author Jannes
 */
public class CrossSection {
    public static Spatial CreateCrossSection(float pixelsPerUnit,Spatial model, RenderManager renderManager, AssetManager assetManager, File textureDir, String name, int lodIndex){
        return CreateCrossSection(pixelsPerUnit, model, renderManager, assetManager, textureDir, name, lodIndex, (BoundingBox)model.getWorldBound());
    }
    //multiple meshes
    /*
    public static Spatial CreateCrossSection(float pixelsPerUnit,Spatial model, RenderManager renderManager, AssetManager assetManager, File textureDir, String name, int lodIndex, BoundingBox modelbound){
        try{
            File crossDir = new File(textureDir, "CrossSections");            
            
            RenderToTexture rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, 0, modelbound);
            BufferedImage biBack = rtt.getImage();                     
            
            rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, FastMath.HALF_PI, modelbound);
            BufferedImage biRight = rtt.getImage();            
            
            rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, FastMath.PI,modelbound);
            BufferedImage biFront= rtt.getImage();            
            
            rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, -FastMath.HALF_PI,modelbound);
            BufferedImage biLeft = rtt.getImage();                       
            
            BoundingBox bb = modelbound;
            Vector3f center = bb.getCenter();
            System.out.printf("center: " + center);
            //int maxWidth = Math.max(biFront.getWidth()+Math.abs((int)(center.x*pixelsPerUnit)), biLeft.getWidth()+Math.abs((int)(center.z*pixelsPerUnit)));
            int maxWidth = Math.max(biFront.getWidth(), biLeft.getWidth());
            BufferedImage img = new BufferedImage(maxWidth*4, biFront.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = img.createGraphics();
            if(biFront.getWidth() > biLeft.getWidth()){  
                //int offset = (int)(center.x*pixelsPerUnit);
                int offset = 0;
                if(offset < 0){
                    g.drawImage(biFront, -offset, 0,biFront.getWidth(),biFront.getHeight(), null);
                    g.drawImage(biBack,maxWidth+offset,0,biBack.getWidth(),biBack.getHeight(), null);
                }else{
                    g.drawImage(biFront,-offset,0,biFront.getWidth(),biFront.getHeight(),null);
                    g.drawImage(biBack,maxWidth+offset,0,biFront.getWidth(),biBack.getHeight(),null);
                }
                int half_diff = (biFront.getWidth()-biLeft.getWidth())/2;
                //offset = (int)(center.z*pixelsPerUnit);
                offset = 0;
                if(offset < 0){
                    g.drawImage(biLeft,maxWidth*2-offset+half_diff,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3+offset+half_diff,0,biRight.getWidth(),biRight.getHeight(),null);
                }else{
                    g.drawImage(biLeft,maxWidth*2-offset+half_diff,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3-offset+half_diff,0,biRight.getWidth(),biRight.getHeight(),null);
                }                
            }else{
                int half_diff = (biLeft.getWidth()-biFront.getWidth())/2;
                //int offset = (int)(center.x*pixelsPerUnit);
                int offset = 0; 
                if(offset < 0){
                    g.drawImage(biFront, 0+half_diff, 0,biFront.getWidth(),biFront.getHeight(), null);
                    g.drawImage(biBack,maxWidth-offset+half_diff,0,biBack.getWidth(),biBack.getHeight(), null);
                }else{
                    g.drawImage(biFront,offset+half_diff,0,biFront.getWidth(),biFront.getHeight(),null);
                    g.drawImage(biBack,maxWidth+half_diff,0,biBack.getWidth(),biBack.getHeight(),null);
                }
                //offset = (int)(center.z*pixelsPerUnit);
                offset = 0;
                if(offset < 0){
                    g.drawImage(biLeft,maxWidth*2,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3-offset,0,biRight.getWidth(),biRight.getHeight(),null);
                }else{
                    g.drawImage(biLeft,maxWidth*2+offset,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3,0,biRight.getWidth(),biRight.getHeight(),null);
                }                
            }            
            g.dispose();
            File texFile = new File(crossDir,name + ".png");
            texFile.getParentFile().mkdirs();
            boolean writingDone = false;
            while(!writingDone){
                try{
                    ImageIO.write(img, "png", texFile);
                    writingDone = true;
                }catch(Exception exc){}
            }
            writingDone = false;
            while(!writingDone){
                try{
                    ImageIO.write(biFront, "png", new File(crossDir,"testFront.png"));
                    writingDone = true;
                }catch(Exception exc){}
            }
             writingDone = false;
            while(!writingDone){
                try{
                    ImageIO.write(biBack, "png", new File(crossDir,"testBack.png"));
                    writingDone = true;
                }catch(Exception exc){}
            }
             writingDone = false;
            while(!writingDone){
                try{
                    ImageIO.write(biLeft, "png", new File(crossDir,"testLeft.png"));
                    writingDone = true;
                }catch(Exception exc){}
            }
             writingDone = false;
            while(!writingDone){
                try{
                    ImageIO.write(biRight, "png", new File(crossDir,"testRight.png"));
                    writingDone = true;
                }catch(Exception exc){}
            }
            

            Vector3f maxValues = new Vector3f();
            bb.getMax(maxValues);
            Vector3f minValues = new Vector3f();
            bb.getMin(minValues);            
            float modelWidth = maxValues.x-minValues.x;            
            float modelHeight = maxValues.y-minValues.y;                        
            float modelDepth = maxValues.z-minValues.z;
            float width = Math.max(modelWidth, modelDepth);            
            Vector3f[] positionBuffer = new Vector3f[8];   
            Vector2f[] textureBuffer = new Vector2f[8];
            
            //texture; from left to right: front, back,left,right
            Mesh mesh;
            Geometry geo;
            int[] indexes = {2,0,1,1,3,2, 6,5,4,5,6,7};            
            String texturePath = texFile.getAbsolutePath();
            texturePath = texturePath.replace("\\","/");
            int splitPoint = texturePath.indexOf("assets/", 0) + 7;
            texturePath = texturePath.substring(splitPoint);            
            Material mat = createMaterial(texturePath, assetManager);
            
            VertexBuffer[] lods = new VertexBuffer[lodIndex+1];
            for(int i = 0; i < lodIndex; i++){
                lods[i] = new VertexBuffer(Type.Index);
                lods[i].setupData(Usage.Dynamic, 1, Format.UnsignedInt, BufferUtils.createIntBuffer(new int[0]));
            }            
            lods[lodIndex] = new VertexBuffer(Type.Index);
            lods[lodIndex].setupData(Usage.Dynamic, 1, Format.UnsignedInt, BufferUtils.createIntBuffer(indexes));                
            
            //front quads 
            positionBuffer[0] = new Vector3f(0,0,0);            
            positionBuffer[1] = new Vector3f(0,0,width/2);
            positionBuffer[2] = new Vector3f(0,modelHeight,0);
            positionBuffer[3] = new Vector3f(0,modelHeight,width/2);       
            float xStart = ((float)(maxWidth*2.5))/img.getWidth();
            float xEnd = ((float)(maxWidth*3))/img.getWidth();
            textureBuffer[0] = new Vector2f(xStart,0);
            textureBuffer[1] = new Vector2f(xEnd,0);
            textureBuffer[2] = new Vector2f(xStart,1.0f);
            textureBuffer[3] = new Vector2f(xEnd,1.0f);
            
            positionBuffer[4] = new Vector3f(0,0,0);
            positionBuffer[5] = new Vector3f(0,0,width/2);
            positionBuffer[6] = new Vector3f(0,modelHeight,0);
            positionBuffer[7] = new Vector3f(0,modelHeight,width/2);           
            xStart = ((float)(maxWidth*3.5))/img.getWidth();
            xEnd = ((float)(maxWidth*3))/img.getWidth();
            textureBuffer[4] = new Vector2f(xStart,0);
            textureBuffer[5] = new Vector2f(xEnd,0);
            textureBuffer[6] = new Vector2f(xStart,1.0f);
            textureBuffer[7] = new Vector2f(xEnd,1.0f);
            
            Node crossSection = new Node("cross_section");
            mesh = new Mesh();            
            mesh.setBuffer(Type.Position,3, BufferUtils.createFloatBuffer(positionBuffer));                
            mesh.setBuffer(Type.TexCoord,2, BufferUtils.createFloatBuffer(textureBuffer));                
            mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));
            mesh.setLodLevels(lods);        
            mesh.updateBound();
            geo = new Geometry("front",mesh);
            geo.setMaterial(mat);
            geo.setQueueBucket(Bucket.Transparent);            
            crossSection.attachChild(geo);
            
            //left quads
            positionBuffer[0] = new Vector3f(0,0,0);
            positionBuffer[1] = new Vector3f(-width/2,0,0);
            positionBuffer[2] = new Vector3f(0,modelHeight,0);            
            positionBuffer[3] = new Vector3f(-width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*0))/img.getWidth();
            textureBuffer[0] = new Vector2f(xStart,0);
            textureBuffer[1] = new Vector2f(xEnd,0);
            textureBuffer[2] = new Vector2f(xStart,1.0f);
            textureBuffer[3] = new Vector2f(xEnd,1.0f);
                        
            positionBuffer[4] = new Vector3f(0,0,0);
            positionBuffer[5] = new Vector3f(-width/2,0,0);
            positionBuffer[6] = new Vector3f(0,modelHeight,0);            
            positionBuffer[7] = new Vector3f(-width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*0))/img.getWidth();
            textureBuffer[4] = new Vector2f(xStart,0);
            textureBuffer[5] = new Vector2f(xEnd,0);
            textureBuffer[6] = new Vector2f(xStart,1.0f);
            textureBuffer[7] = new Vector2f(xEnd,1.0f);
            
            mesh = new Mesh();            
            mesh.setBuffer(Type.Position,3, BufferUtils.createFloatBuffer(positionBuffer));                
            mesh.setBuffer(Type.TexCoord,2, BufferUtils.createFloatBuffer(textureBuffer));                
            mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));
            mesh.setLodLevels(lods);        
            mesh.updateBound();
            geo = new Geometry("left",mesh);
            geo.setMaterial(mat);
            geo.setQueueBucket(Bucket.Transparent);
            crossSection.attachChild(geo);
            
            //right quad front
            positionBuffer[0] = new Vector3f(0,0,0);
            positionBuffer[1] = new Vector3f(width/2,0,0);
            positionBuffer[2] = new Vector3f(0,modelHeight,0);            
            positionBuffer[3] = new Vector3f(width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*1.0))/img.getWidth();
            textureBuffer[0] = new Vector2f(xStart,0);
            textureBuffer[1] = new Vector2f(xEnd,0);
            textureBuffer[2] = new Vector2f(xStart,1.0f);
            textureBuffer[3] = new Vector2f(xEnd,1.0f);
                        
            positionBuffer[4] = new Vector3f(0,0,0);
            positionBuffer[5] = new Vector3f(width/2,0,0);
            positionBuffer[6] = new Vector3f(0,modelHeight,0);            
            positionBuffer[7] = new Vector3f(width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*1.0))/img.getWidth();
            textureBuffer[4] = new Vector2f(xStart,0);
            textureBuffer[5] = new Vector2f(xEnd,0);
            textureBuffer[6] = new Vector2f(xStart,1.0f);
            textureBuffer[7] = new Vector2f(xEnd,1.0f);
            
            mesh = new Mesh();            
            mesh.setBuffer(Type.Position,3, BufferUtils.createFloatBuffer(positionBuffer));                
            mesh.setBuffer(Type.TexCoord,2, BufferUtils.createFloatBuffer(textureBuffer));                
            mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));
            mesh.setLodLevels(lods);        
            mesh.updateBound();
            geo = new Geometry("right",mesh);
            geo.setMaterial(mat);
            geo.setQueueBucket(Bucket.Transparent);
            crossSection.attachChild(geo);
            
            //two back quads
            positionBuffer[0] = new Vector3f(0,0,0);
            positionBuffer[1] = new Vector3f(0,0,-width/2);
            positionBuffer[2] = new Vector3f(0,modelHeight,0);
            positionBuffer[3] = new Vector3f(0,modelHeight,-width/2);
            xStart = ((float)(maxWidth*2.5))/img.getWidth();
            xEnd = ((float)(maxWidth*2))/img.getWidth();
            textureBuffer[0] = new Vector2f(xStart,0);
            textureBuffer[1] = new Vector2f(xEnd,0);
            textureBuffer[2] = new Vector2f(xStart,1.0f);
            textureBuffer[3] = new Vector2f(xEnd,1.0f);
            
            positionBuffer[4] = new Vector3f(0,0,0);
            positionBuffer[5] = new Vector3f(0,0,-width/2);
            positionBuffer[6] = new Vector3f(0,modelHeight,0);
            positionBuffer[7] = new Vector3f(0,modelHeight,-width/2);   
            xStart = ((float)(maxWidth*3.5))/img.getWidth();
            xEnd = ((float)(maxWidth*4))/img.getWidth();
            textureBuffer[4] = new Vector2f(xStart,0);
            textureBuffer[5] = new Vector2f(xEnd,0);
            textureBuffer[6] = new Vector2f(xStart,1.0f);
            textureBuffer[7] = new Vector2f(xEnd,1.0f);                        
            
            mesh = new Mesh();            
            mesh.setBuffer(Type.Position,3, BufferUtils.createFloatBuffer(positionBuffer));                
            mesh.setBuffer(Type.TexCoord,2, BufferUtils.createFloatBuffer(textureBuffer));                
            mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes)); 
            mesh.setLodLevels(lods);        
            mesh.updateBound();
            geo = new Geometry("back",mesh);
            geo.setMaterial(mat);
            geo.setQueueBucket(Bucket.Transparent);
            crossSection.attachChild(geo); 
            crossSection.updateModelBound();
            crossSection.move(bb.getCenter());
            crossSection.move(0,-modelHeight/2,0);
            
            return crossSection;
        }catch(Exception exc){
            StringWriter sw = new StringWriter();
            PrintWriter pw = new PrintWriter(sw);
            exc.printStackTrace(pw);
            JOptionPane.showMessageDialog(null, "error creating crosssection: " + sw.toString());            
        }
        return null;
    }    
    */
    //only one mesh
    public static Spatial CreateCrossSection(float pixelsPerUnit,Spatial model, RenderManager renderManager, AssetManager assetManager, File textureDir, String name, int lodIndex, BoundingBox modelbound){
        try{
            File crossDir = new File(textureDir, "CrossSections");            
            
            RenderToTexture rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, 0, modelbound);
            BufferedImage biBack = rtt.getImage();                     
            
            rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, FastMath.HALF_PI, modelbound);
            BufferedImage biRight = rtt.getImage();            
            
            rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, FastMath.PI,modelbound);
            BufferedImage biFront= rtt.getImage();            
            
            rtt = new RenderToTexture(pixelsPerUnit, renderManager,model, -FastMath.HALF_PI,modelbound);
            BufferedImage biLeft = rtt.getImage();                       
            
            BoundingBox bb = modelbound;
            Vector3f center = bb.getCenter();
            System.out.printf("center: " + center);
            //int maxWidth = Math.max(biFront.getWidth()+Math.abs((int)(center.x*pixelsPerUnit)), biLeft.getWidth()+Math.abs((int)(center.z*pixelsPerUnit)));
            int maxWidth = Math.max(biFront.getWidth(), biLeft.getWidth());
            BufferedImage img = new BufferedImage(maxWidth*4, biFront.getHeight(), BufferedImage.TYPE_INT_ARGB);
            Graphics g = img.createGraphics();
            if(biFront.getWidth() > biLeft.getWidth()){  
                //int offset = (int)(center.x*pixelsPerUnit);
                int offset = 0;
                if(offset < 0){
                    g.drawImage(biFront, -offset, 0,biFront.getWidth(),biFront.getHeight(), null);
                    g.drawImage(biBack,maxWidth+offset,0,biBack.getWidth(),biBack.getHeight(), null);
                }else{
                    g.drawImage(biFront,-offset,0,biFront.getWidth(),biFront.getHeight(),null);
                    g.drawImage(biBack,maxWidth+offset,0,biFront.getWidth(),biBack.getHeight(),null);
                }
                int half_diff = (biFront.getWidth()-biLeft.getWidth())/2;
                //offset = (int)(center.z*pixelsPerUnit);
                offset = 0;
                if(offset < 0){
                    g.drawImage(biLeft,maxWidth*2-offset+half_diff,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3+offset+half_diff,0,biRight.getWidth(),biRight.getHeight(),null);
                }else{
                    g.drawImage(biLeft,maxWidth*2-offset+half_diff,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3-offset+half_diff,0,biRight.getWidth(),biRight.getHeight(),null);
                }                
            }else{
                int half_diff = (biLeft.getWidth()-biFront.getWidth())/2;
                //int offset = (int)(center.x*pixelsPerUnit);
                int offset = 0; 
                if(offset < 0){
                    g.drawImage(biFront, 0+half_diff, 0,biFront.getWidth(),biFront.getHeight(), null);
                    g.drawImage(biBack,maxWidth-offset+half_diff,0,biBack.getWidth(),biBack.getHeight(), null);
                }else{
                    g.drawImage(biFront,offset+half_diff,0,biFront.getWidth(),biFront.getHeight(),null);
                    g.drawImage(biBack,maxWidth+half_diff,0,biBack.getWidth(),biBack.getHeight(),null);
                }
                //offset = (int)(center.z*pixelsPerUnit);
                offset = 0;
                if(offset < 0){
                    g.drawImage(biLeft,maxWidth*2,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3-offset,0,biRight.getWidth(),biRight.getHeight(),null);
                }else{
                    g.drawImage(biLeft,maxWidth*2+offset,0,biLeft.getWidth(),biLeft.getHeight(),null);
                    g.drawImage(biRight,maxWidth*3,0,biRight.getWidth(),biRight.getHeight(),null);
                }                
            }            
            g.dispose();
            File texFile = new File(crossDir,name + ".png");
            texFile.getParentFile().mkdirs();
            boolean writingDone = false;
            while(!writingDone){
                try{
                    ImageIO.write(img, "png", texFile);
                    writingDone = true;
                }catch(Exception exc){}
            }
            
            Vector3f maxValues = new Vector3f();
            bb.getMax(maxValues);
            Vector3f minValues = new Vector3f();
            bb.getMin(minValues);            
            float modelWidth = maxValues.x-minValues.x;            
            float modelHeight = maxValues.y - minValues.y;                        
            float modelDepth = maxValues.z-minValues.z;
            float width = Math.max(modelWidth, modelDepth);
            Mesh mesh = new Mesh();
            Vector3f[] positionBuffer = new Vector3f[32];   
            Vector2f[] textureBuffer = new Vector2f[32];
            //texture; from left to right: front, back,left,right
            
            //two front quads 
            positionBuffer[0] = new Vector3f(0,0,0);            
            positionBuffer[1] = new Vector3f(0,0,width/2);
            positionBuffer[2] = new Vector3f(0,modelHeight,0);
            positionBuffer[3] = new Vector3f(0,modelHeight,width/2);       
            float xStart = ((float)(maxWidth*2.5))/img.getWidth();
            float xEnd = ((float)(maxWidth*3))/img.getWidth();
            textureBuffer[0] = new Vector2f(xStart,0);
            textureBuffer[1] = new Vector2f(xEnd,0);
            textureBuffer[2] = new Vector2f(xStart,1.0f);
            textureBuffer[3] = new Vector2f(xEnd,1.0f);
            
            positionBuffer[4] = new Vector3f(0,0,0);
            positionBuffer[5] = new Vector3f(0,0,width/2);
            positionBuffer[6] = new Vector3f(0,modelHeight,0);
            positionBuffer[7] = new Vector3f(0,modelHeight,width/2);           
            xStart = ((float)(maxWidth*3.5))/img.getWidth();
            xEnd = ((float)(maxWidth*3))/img.getWidth();
            textureBuffer[4] = new Vector2f(xStart,0);
            textureBuffer[5] = new Vector2f(xEnd,0);
            textureBuffer[6] = new Vector2f(xStart,1.0f);
            textureBuffer[7] = new Vector2f(xEnd,1.0f);
            
            //left quad front                                   
            positionBuffer[8] = new Vector3f(0,0,0);
            positionBuffer[9] = new Vector3f(-width/2,0,0);
            positionBuffer[10] = new Vector3f(0,modelHeight,0);            
            positionBuffer[11] = new Vector3f(-width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*0))/img.getWidth();
            textureBuffer[8] = new Vector2f(xStart,0);
            textureBuffer[9] = new Vector2f(xEnd,0);
            textureBuffer[10] = new Vector2f(xStart,1.0f);
            textureBuffer[11] = new Vector2f(xEnd,1.0f);
            
            //right quad front
            positionBuffer[12] = new Vector3f(0,0,0);
            positionBuffer[13] = new Vector3f(width/2,0,0);
            positionBuffer[14] = new Vector3f(0,modelHeight,0);            
            positionBuffer[15] = new Vector3f(width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*1.0))/img.getWidth();
            textureBuffer[12] = new Vector2f(xStart,0);
            textureBuffer[13] = new Vector2f(xEnd,0);
            textureBuffer[14] = new Vector2f(xStart,1.0f);
            textureBuffer[15] = new Vector2f(xEnd,1.0f);
            
            
            //two back quads
            positionBuffer[16] = new Vector3f(0,0,0);
            positionBuffer[17] = new Vector3f(0,0,-width/2);
            positionBuffer[18] = new Vector3f(0,modelHeight,0);
            positionBuffer[19] = new Vector3f(0,modelHeight,-width/2);
            xStart = ((float)(maxWidth*2.5))/img.getWidth();
            xEnd = ((float)(maxWidth*2))/img.getWidth();
            textureBuffer[16] = new Vector2f(xStart,0);
            textureBuffer[17] = new Vector2f(xEnd,0);
            textureBuffer[18] = new Vector2f(xStart,1.0f);
            textureBuffer[19] = new Vector2f(xEnd,1.0f);
            
            positionBuffer[20] = new Vector3f(0,0,0);
            positionBuffer[21] = new Vector3f(0,0,-width/2);
            positionBuffer[22] = new Vector3f(0,modelHeight,0);
            positionBuffer[23] = new Vector3f(0,modelHeight,-width/2);   
            xStart = ((float)(maxWidth*3.5))/img.getWidth();
            xEnd = ((float)(maxWidth*4))/img.getWidth();
            textureBuffer[20] = new Vector2f(xStart,0);
            textureBuffer[21] = new Vector2f(xEnd,0);
            textureBuffer[22] = new Vector2f(xStart,1.0f);
            textureBuffer[23] = new Vector2f(xEnd,1.0f);                        
            
            //left quad back                                   
            positionBuffer[24] = new Vector3f(0,0,0);
            positionBuffer[25] = new Vector3f(-width/2,0,0);
            positionBuffer[26] = new Vector3f(0,modelHeight,0);            
            positionBuffer[27] = new Vector3f(-width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*0))/img.getWidth();
            textureBuffer[24] = new Vector2f(xStart,0);
            textureBuffer[25] = new Vector2f(xEnd,0);
            textureBuffer[26] = new Vector2f(xStart,1.0f);
            textureBuffer[27] = new Vector2f(xEnd,1.0f);
            
            //right quad back 
            positionBuffer[28] = new Vector3f(0,0,0);
            positionBuffer[29] = new Vector3f(width/2,0,0);
            positionBuffer[30] = new Vector3f(0,modelHeight,0);            
            positionBuffer[31] = new Vector3f(width/2,modelHeight,0);
            xStart = ((float)(maxWidth*0.5))/img.getWidth();
            xEnd = ((float)(maxWidth*1.0))/img.getWidth();
            textureBuffer[28] = new Vector2f(xStart,0);
            textureBuffer[29] = new Vector2f(xEnd,0);
            textureBuffer[30] = new Vector2f(xStart,1.0f);
            textureBuffer[31] = new Vector2f(xEnd,1.0f);
            
            int[] indexes = {22,20,21,21,23,22, 18,17,16,17,18,19, 10,9,8,9,10,11, 14,12,13,13,15,14, 2,0,1,1,3,2, 6,5,4,5,6,7};            
            mesh.setBuffer(Type.Position,3, BufferUtils.createFloatBuffer(positionBuffer));                
            mesh.setBuffer(Type.TexCoord,2, BufferUtils.createFloatBuffer(textureBuffer));                
            mesh.setBuffer(Type.Index, 1, BufferUtils.createIntBuffer(indexes));                                
            mesh.updateBound();
            
            VertexBuffer[] lods = new VertexBuffer[3];
            lods[0] = new VertexBuffer(Type.Index);
            lods[0].setupData(Usage.Dynamic, 1, Format.UnsignedInt, BufferUtils.createIntBuffer(new int[0]));
            lods[1] = new VertexBuffer(Type.Index);
            lods[1].setupData(Usage.Dynamic, 1, Format.UnsignedInt, BufferUtils.createIntBuffer(new int[0]));    
            lods[2] = new VertexBuffer(Type.Index);
            lods[2].setupData(Usage.Dynamic, 1, Format.UnsignedInt, BufferUtils.createIntBuffer(indexes));    
            mesh.setLodLevels(lods);        

            Geometry crossSection = new Geometry("cross" , mesh);
            String texturePath = texFile.getAbsolutePath();
            texturePath = texturePath.replace("\\","/");
            int splitPoint = texturePath.indexOf("assets/", 0) + 7;
            texturePath = texturePath.substring(splitPoint);            
            crossSection.setMaterial(createMaterial(texturePath, assetManager));
            crossSection.updateModelBound();
            crossSection.addControl(new CrossSectionControl());
            crossSection.move(bb.getCenter());
            crossSection.move(0,-modelHeight/2,0);
            crossSection.setQueueBucket(Bucket.Transparent);
            return crossSection;
        }catch(Exception exc){
            JOptionPane.showMessageDialog(null, "error creating crossection: " + exc);
        }
        return null;
    } 
    private static Material createMaterial(String texturePath,AssetManager assetManager){
        Material mat = null;        
        mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");                                 
        Texture texture = assetManager.loadTexture(texturePath);
        mat.setTexture("ColorMap", texture);
        mat.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);        
        mat.getAdditionalRenderState().setDepthWrite(false);
        return mat;
    }
}
